package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

/*正五边形*/
class RegularPentagonView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private var mCx = 0F
    private var mCy = 0F
    private var mCr = 0F

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //拐角---(尖角)
            strokeJoin = Paint.Join.MITER
            strokeWidth = context.resources.getDimension(R.dimen.dp_1)
            color = context.resources.getColor(R.color.color_B128C9)
            style = Paint.Style.STROKE
        }
    }

    //private val degrees = floatArrayOf(180F, 360F)
    //private val degrees = floatArrayOf(120F, 240F, 360F)
    //private val degrees = floatArrayOf(90F, 180F, 270F, 360F)
    private val degrees = floatArrayOf(72F, 144F, 216F, 288F, 360F)
    //private val degrees = floatArrayOf(60F, 120F, 180F, 240F, 300F, 360F)
    //private val degrees = floatArrayOf(40F, 80F, 120F, 160F, 200F, 240F, 280F, 320F, 360F)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d(TAG, "override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)")
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_60).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_60).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        Log.d(TAG, "override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)")
        mCx = measuredWidth / 2.0F
        mCy = measuredHeight / 2.0F
        mCr = measuredWidth.coerceAtLeast(measuredHeight) * 9.0F / 20.0F
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            translate(mWidth / 2.0F, mHeight / 2.0F)
            mCx = 0F
            mCy = 0F
            val distance = 2 * mCr * Math.sin(Math.toRadians(degrees[0] / 2.0)).toFloat()
            val cValue = /*mCy*/ - mCr
            val stopY =
                (distance * distance /*+ mCy * mCy*/ - cValue * cValue - mCr * mCr) / (2 * (/*mCy*/ - cValue))
            val stopX =
                (/*mCx + */Math.sqrt(-1.0 * cValue * cValue + 2 * cValue * stopY + distance * distance - stopY * stopY)).toFloat()
            for (angle in degrees) {
                save()
                rotate(angle/*, mCx, mCy*/)
                drawLine(mCx, cValue, stopX, stopY, mPaint0)
                restore()
            }
        }
    }

    companion object {
        const val TAG = "RegularPentagonView"
    }
}